'use strict';

app.controller('event_detail_Ctrl', ['$scope','$http','loginService','$routeParams', function($scope,$http,loginService,$routeParams){
	
	    //=====================paging list members of event======================
		$scope.nbr_rows_attendings=0;
	    $scope.entryLimit_event = 5
	    $scope.offset_event = 0;
	    $scope.disablePrev_event = true;
	    $scope.disableNext_event = false;
	    $scope.page_now_event = 1;
	    //=======================================================================
	 $scope.init = function() {
		     loginService.islogged(); 
			  $scope.link = loginService.getLink();
	    	 $scope.getEventByEventId($routeParams.id);
	    };
	
	 $scope.getEventByEventId = function(id){
			        $http.get($scope.link+'event/'+id).success(function(data){
			        	console.log('an event is loaded');
			        	$scope.event = data;
			        	$scope.getUserById(data.data.owner_id,1);
						$scope.cities = [];
			        	var loc = data.data.location;
			        	var loc_Str = loc.toString();
			        	var arr  = loc_Str.split(",") ; 
			        	$scope.long = arr[1];
			        	$scope.lat = arr[0];
			        	
			        	$scope.getUsersByEventId(id);
			        	$scope.getGroupByGroupId(data.data.group_id);
						
						 $scope.cities.push({ 
								"name" : data.data.name,
								"interest"  : data.data.interest,
								"lat" : arr[0],
								"long" : arr[1]
							});
						$scope.getNearByMap();
						
			        }).error(function(data, status, config) {
			        	console.log('can not load event with event id : ' + id);
			 });
	}
	
	
	 $scope.getNearByMap = function(){
				 var mapOptions = {
				zoom: 12,
				center: new google.maps.LatLng($scope.lat, $scope.long),
				mapTypeId: google.maps.MapTypeId.TERRAIN
			}

			$scope.map = new google.maps.Map(document.getElementById('map'), mapOptions);

			$scope.markers = [];
			
			 var infoWindow = new google.maps.InfoWindow();
			 
			  var createMarker = function (info){
				
				var marker = new google.maps.Marker({
					map: $scope.map,
					position: new google.maps.LatLng(info.lat, info.long),
					title: info.name
				});
				marker.content = '<div class="infoWindowContent">' + info.interest + '</div>';
				
				google.maps.event.addListener(marker, 'click', function(){
					infoWindow.setContent('<h2>' + marker.title + '</h2>' + marker.content);
					infoWindow.open($scope.map, marker);
				});
				
				$scope.markers.push(marker);
				
			}
			
			
			 //for (i = 0; i < cities.length; i++){
				createMarker($scope.cities[0]);
			//}
		  
			  $scope.openInfoWindow = function(e, selectedMarker){
				e.preventDefault();
				google.maps.event.trigger(selectedMarker, 'click');
			}
		 
	 };
	
	
		 $scope.getGroupByGroupId = function(id){
			        $http.get($scope.link+'group/'+id).success(function(data){
			        	console.log('a group is loaded');
			        }).success(function(data, status, config) {
			        	 $scope.name_gr = data.data.name;
			 });
			 }
	
	$scope.getUsersByEventId = function(id){ 
			        $http.get($scope.link+'user_event/'+id).success(function(data){
			        	console.log('list users by event id is loaded');
			        	$scope.lst_user = data;
			        	
			        	$scope.nb_users = data.data.attendings_list.length;
			        	//alert($scope.nb_users);
			        	
                    	$scope.jsonToArray_members(data);
			        	pageResult_event($scope);
			        }).error(function(data, status, config) {
			        	console.log('can not load list of users by event Id : ' + id);
			 });
	}
	
	
	 $scope.jsonToArray_members = function (data) { //alert(JSON.stringify(data));
					$scope.b = new Array();
					var i = 0;
					angular.forEach(data.data.attendings_list,function(){
						$scope.b[i]=new Array(2);
			    		
						$scope.b[i][1] =  data.data.attendings_list[i].id;
						//var id = data.data.attendings_list[i].id;
						//$scope.getUserById(id,2);
						$scope.b[i][0] =  data.data.attendings_list[i].name;
						
			            i++;
			        })
			        $scope.nbr_rows_attendings = i ;
					$scope.b_ini= angular.copy($scope.b);
	 }
    
	  $scope.$watch('b', function() {   
			if( ($scope.page_now_event==1)  &&  ($scope.nbr_rows_attendings <= $scope.entryLimit_event) )
				 {
				 $scope.disableNext_event = true;
				 }
				
			}); 
	 $scope.getUserById = function(id, type){
				var name = 'not_name';
			        $http.get($scope.link+'users/'+id).success(function(data){
			        	console.log('owner is loaded');
			            if(type==1){
			        	  $scope.owner = data;
			            }
			        	
			         
			        	 //$scope.getGroupsByUserId(data.data.id);
			        }).success(function(data, status, config) {
			        	 $scope.name = data.data.first_name + ' ' + data.data.last_name;
			        });
			    
	 }
	
	// switch to next page
	$scope.nextPage_event = function() { 
		nextPage_event_users($scope);
	}
		
	// switch to previous page
	$scope.previousPage_event = function() {
		previousPage_event_users($scope);
	}
	
}])